package com.wanxi.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanxi.VO.NewsVO;
import com.wanxi.dao.NewsDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.entity.NewsEntity;
import com.wanxi.entity.UserEntity;
import com.wanxi.service.NewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/28 22:02
 */
@Service("newsService")
public class NewsServiceImpl implements NewsService {
    @Autowired
    NewsDao newsDao;
    @Autowired
    NewsEntity newsEntity;

    Jedis jedis = new Jedis();
    public ResultDTO findAll(NewsVO newsVO) {
//        NewsEntity newsEntity = new NewsEntity();
        ObjectMapper mapper = new ObjectMapper();
        BeanUtils.copyProperties(newsVO,newsEntity);
        int count = newsDao.count(newsEntity);
        List<NewsEntity> list = new ArrayList<>();
        if (newsEntity.getPage() != null && newsEntity.getPage() == 1 && jedis.exists("newsList")){
            String jedisNewsList = jedis.get("newsList");
            //反序列化数组，方法2
            JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, NewsEntity.class);
            try {
                list = mapper.readValue(jedisNewsList, javaType);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }else{
            list = newsDao.findAll(newsEntity);
            String newsList = null;
            try {
                newsList = mapper.writeValueAsString(list);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            jedis.set("newsList",newsList);
        }

        return ResultDTO.getResult("", count, list);
    }
    public ResultDTO add(NewsVO newsVO) {
//        NewsEntity newsEntity = new NewsEntity();
        BeanUtils.copyProperties(newsVO,newsEntity);
        int count = newsDao.add(newsEntity);
        jedis.del("newsList");
        return ResultDTO.getResult(count);
    }

    public ResultDTO delete(NewsVO newsVO) {
//        NewsEntity newsEntity = new NewsEntity();
        BeanUtils.copyProperties(newsVO,newsEntity);
        int delete = newsDao.delete(newsEntity);
        jedis.del("newsList");
        return ResultDTO.getResult(delete);
    }

    public ResultDTO findById(NewsVO newsVO) {
//        NewsEntity newsEntity = new NewsEntity();
        BeanUtils.copyProperties(newsVO,newsEntity);
        NewsEntity byId = newsDao.findById(newsEntity);
        return ResultDTO.getResult(byId);
    }



    public ResultDTO update(NewsVO newsVO) {
//        NewsEntity newsEntity = new NewsEntity();
        BeanUtils.copyProperties(newsVO,newsEntity);
        int update = newsDao.update(newsEntity);
        jedis.del("newsList");
        return ResultDTO.getResult(update);
    }
}
